dd.project.ui({
    dd: {
        log: function(message, level)
        {
            if(level == "error")
            {
                alert(level + ": " + message);
            }
            if(typeof console != "undefined")
            {
                console.log(level + ": " + message);
            }
        },
        selector: function(id)
        {
            return new dd.project.ui._selector[id].self.fn(dd.project.ui._selector[id]);
        },
        controller: function(o)
        {
            o[this.current.action].call({});
        }
    },
    init: function()
    {
        dd._defs = {linearGradient:[]};
        dd.defs = function(o)
        {
            if(o.hasOwnProperty("linearGradient"))
            {
                for(var i in o.linearGradient)
                {
                    this._defs.linearGradient[i] = o.linearGradient[i];
                }
            }
        };
        dd.control("ui", function(x, y)
        {
            this.fn.parent = dd(this.parent).addControl("svg");
            dd(this.fn.parent).defs(dd._defs);
            this.method("navigate", (function(p){
                return function(action, controller, o){
                    var file = "scripts/uip/view/" + controller + "/" + action + ".xml";
                    dd.xml({
                        url: file,
                        success: function()
                        {
                            dd.project.ui._selector = [];
                            dd.project.ui.render(this.xml, p);
                            dd.current.action = action;
                            dd.current.controller = controller;
                            dd.addJS("scripts/uip/controller/" + controller + ".js");
                        },
                        failure: function()
                        {
                            dd.log("Syntax error in \"" + file + "\"", "error");
                        }
                    }, { parent: this.self.parent });
                };
            })(this));
            this.method("addControl", this.addControl);
            this.method("empty", function()
            {
                dd(this.fn.parent).empty(); // Remove Elements
            });
        });
    },
    render: function(nodes, parent)
    {
        for(var i = 0; i < nodes.children.length; i++)
        {
            var index = nodes.children[i].index;
            var tagName = nodes.children[i].tagName;
            var id = nodes[tagName][index].getAttribute("id") || this.guid();
            dd.log(tagName + " " + id, "log");
            var attr = nodes[tagName][index].attributes;
            attr.content = nodes[tagName][index].innerText;
            dd.project.ui._selector[id] = dd(parent).addControl(tagName, attr);
            if(nodes[tagName][index].hasOwnProperty("children"))
            { // Add control might be in the wrong scope... ????
                dd.project.ui.render(nodes[tagName][index], dd.project.ui._selector[id].self);
            }
        }
    },
    guid: function()
    {
        var S4 = function()
        {
           return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
        };
        return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
    }
});